package com.rljk.domain.po;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rljk.common.consts.AssetConst;
import com.rljk.utils.DateUtil;
import com.rljk.utils.NetworkUtil;
import com.tencent.trustsql.sdk.Constants;
import com.tencent.trustsql.sdk.ErrorNum;
import com.tencent.trustsql.sdk.TrustSDK;
import com.tencent.trustsql.sdk.exception.TrustSDKException;
import com.tencent.trustsql.sdk.util.HttpClientUtil;
import com.tencent.trustsql.sdk.util.SignStrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Author:   YASUO
 * Date:     2018/7/23 13:29
 * Description:资产发行申请https://baas.trustsql.qq.com/cgi-bin/v1.0/dam_asset_issue_apply_v1.cgi
 */
@Slf4j
public class AssetTransferApply {

    private static String version = "1.0";//接口版本
    private static String sign_type = "ECDSA";//签名方式
    //private static String mch_id = "gba363879924b43db";//通讯方ID
    private static String mch_id = "gbccea88ea44d0a3";//通讯方ID



    private static String node_id = "nd_tencent_test4";//链ID
    private static String chain_id = "ch_tencent_test";//链ID
    private static String ledger_id = "ld_tencent_iss";//账本ID
    private static String owner_account = "1HhkpqCzSfpo1Du7RXd6dJPVj87XTZQNqx";//持有方帐户
    private static int asset_type = 1;//资产类型
    private static int amount = 10000;//资产份额
    private static String unit = "个";//资产份额
    //private static String dst_account = "gba363879924b43db";//资产份额






    public String execute(String prvKey,Map<String, Object>paramMap) throws Exception {
//        if (args == null || args.length < 4 || StringUtils.isEmpty(args[1]) || StringUtils.isEmpty(args[2])
//                || StringUtils.isEmpty(args[3])) {
//            throw new TrustSDKException(ErrorNum.INVALID_PARAM_ERROR.getRetCode(),
//                    ErrorNum.INVALID_PARAM_ERROR.getRetMsg());
//        }
        //String mch_id = NetworkUtil.getIPAddress(request);
        //String prvKey = args[2];
//        String jsonArr = args[3];
        JSONArray resultJsonArr = new JSONArray();
//        System.out.println("jsonArr: " + jsonArr);
//        JSONObject jsonObj = JSONObject.parseObject(jsonArr);
//        Map<String, Object> paramMap = new TreeMap<String, Object>();
        paramMap.put("version", AssetConst.version);
        paramMap.put("sign_type", AssetConst.sign_type);
        paramMap.put("mch_id", AssetConst.mch_id);
//        if (StringUtils.isNotEmpty(jsonObj.getString("node_id"))) {
//            paramMap.put("node_id", jsonObj.getString("node_id"));
//        }
        paramMap.put("node_id", AssetConst.node_id);
        paramMap.put("chain_id", AssetConst.chain_id);
        paramMap.put("ledger_id", AssetConst.ledger_id);
//        paramMap.put("src_account", jsonObj.getString("src_account"));
        paramMap.put("owner_account", AssetConst.owner_account);
//        paramMap.put("fee_account", jsonObj.getString("fee_account"));
        //paramMap.put("fee_account", jsonObj.getString("fee_account"));
        paramMap.put("asset_type", AssetConst.asset_type);
        paramMap.put("amount", AssetConst.amount);
        paramMap.put("unit", AssetConst.unit);



        Map<String,Object> map = new HashMap<>();
        map.put("info","发布资产");
        Object o = JSONObject.toJSON(map);
        System.out.println("requestParam:"+o.toString());

        paramMap.put("content", o);
        //paramMap.put("fee_amount", jsonObj.getString("fee_amount"));
        //paramMap.put("sign_in_date", DateUtil.getTime());
        //paramMap.put("extra_info", "");
        paramMap.put("timestamp", System.currentTimeMillis() / 1000);       //unix时间戳
        paramMap.put("mch_sign", TrustSDK.signString(prvKey, SignStrUtil.mapToKeyValueStr(paramMap).getBytes(), false));
        // generate post data
        JSONObject postJson = new JSONObject();
        for (String key : paramMap.keySet()) {
            postJson.put(key, paramMap.get(key));
        }

        log.info("postJson:"+postJson.toJSONString());

        String url = "https://baas.trustsql.qq.com/cgi-bin/v1.0/dam_asset_issue_apply_v1.cgi";
        String result = HttpClientUtil.post(url, postJson.toJSONString());
        // 分析http请求结果
        JSONObject resultJson = JSONObject.parseObject(result);

        log.info("分析http请求结果resultJson:"+resultJson);
        if ("0".equals(resultJson.getString("retcode")) && "OK".equals(resultJson.getString("retmsg"))) {
            // 验证返回数据的mch_sign
            paramMap.clear();
            for (Map.Entry<String, Object> entry : resultJson.entrySet()) {
                if (!"mch_sign".equals(entry.getKey())) {
                    paramMap.put(entry.getKey(), entry.getValue());
                }
            }
            boolean mchSignValid = TrustSDK.verifyString(Constants.INFO_SHARE_PUBKEY,
                    SignStrUtil.mapToKeyValueStr(paramMap), resultJson.getString("mch_sign"));
            resultJson.put("mch_sign_verify", mchSignValid);
        }
        resultJsonArr.add(0, resultJson);
        return resultJsonArr.toJSONString();
    }
}
